package com.littlegnal.share2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.littlegnal.share2.db.DbManager;
import com.littlegnal.share2.model.CourseListCollection;
import com.littlegnal.share2.model.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    request();
  }

  void request() {
    ScheduleCourseNetService scheduleCourseNetService =
        ((App) getApplicationContext()).getRetrofit().create(ScheduleCourseNetService.class);

    final Call<CourseListCollection> collection =
        scheduleCourseNetService.getCourseList(1, "", "");
    collection.enqueue(new Callback<CourseListCollection>() {
      @Override
      public void onResponse(
          Call<CourseListCollection> call,
          Response<CourseListCollection> response) {
        CourseListCollection courseListCollection = response.body();
        new DbManager().asyncCreate(courseListCollection, new DbManager.DbManagerListener() {
          @Override
          public void onCompleted() {
            // do something ...
          }
        });
      }

      @Override
      public void onFailure(Call<CourseListCollection> call, Throwable t) {

      }
    });
  }

  void request2() {
    ScheduleCourseNetService scheduleCourseNetService =
        ((App) getApplicationContext()).getRetrofit().create(ScheduleCourseNetService.class);

    scheduleCourseNetService.getCourseListObservable(1, "", "")
        .doOnNext(new Action1<CourseListCollection>() {
          @Override
          public void call(CourseListCollection courseListCollection) {
            new DbManager().create(courseListCollection);
          }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<CourseListCollection>() {
          @Override
          public void onCompleted() {

          }

          @Override
          public void onError(Throwable e) {

          }

          @Override
          public void onNext(CourseListCollection courseListCollection) {
            // do something ...
          }
        });

  }

  void request3() {
    final ScheduleCourseNetService scheduleCourseNetService =
        ((App) getApplicationContext()).getRetrofit().create(ScheduleCourseNetService.class);
    Call<User> call = scheduleCourseNetService.login("Bill", "123456");
    call.enqueue(new Callback<User>() {
      @Override
      public void onResponse(Call<User> call, Response<User> response) {
        new DbManager().asyncCreate(response.body(), new DbManager.DbManagerListener() {
          @Override
          public void onCompleted() {
            Call<CourseListCollection> courseListCollectionCall =
                scheduleCourseNetService.getCourseList(1, "", "");
            courseListCollectionCall.enqueue(new Callback<CourseListCollection>() {
              @Override
              public void onResponse(
                  Call<CourseListCollection> call,
                  Response<CourseListCollection> response) {
                new DbManager().asyncCreate(response.body(), new DbManager.DbManagerListener() {
                  @Override
                  public void onCompleted() {
                    // do something ...
                  }
                });
              }

              @Override
              public void onFailure(Call<CourseListCollection> call, Throwable t) {

              }
            });
          }
        });
      }

      @Override
      public void onFailure(Call<User> call, Throwable t) {

      }
    });
  }

  void request4() {
    final ScheduleCourseNetService scheduleCourseNetService =
        ((App) getApplicationContext()).getRetrofit().create(ScheduleCourseNetService.class);

    scheduleCourseNetService.loginObservable("Bill", "123")
        .doOnNext(new Action1<User>() {
          @Override
          public void call(User user) {
            new DbManager().create(user);
          }
        })
        .flatMap(new Func1<User, Observable<CourseListCollection>>() {
          @Override
          public Observable<CourseListCollection> call(User user) {
            return scheduleCourseNetService.getCourseListObservable(1, "", "");
          }
        })
        .doOnNext(new Action1<CourseListCollection>() {
          @Override
          public void call(CourseListCollection courseListCollection) {
            new DbManager().create(courseListCollection);
          }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<CourseListCollection>() {
          @Override
          public void onCompleted() {

          }

          @Override
          public void onError(Throwable e) {

          }

          @Override
          public void onNext(CourseListCollection courseListCollection) {
            // do something ...
          }
        });
  }
}
